package cn.xjt;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Auther: Xujt
 * @Date: 2022/8/31 16:05
 * @Description:
 */
public class GenCode {

    public static List<String> tableNames = new ArrayList<>();

    static {
        tableNames.add("supply_express");
    }

    static boolean isRuoYiGen = true;

    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/wanjing_supply"
                , "root", "root")
                .globalConfig(builder -> {
                    builder.author("Xujt") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .outputDir(System.getProperty("user.dir")+"/src/main/java"); // 指定输出目录
                    builder.dateType(DateType.TIME_PACK);
                    builder.commentDate("yyyy-MM-dd HH:mm:ss");
                    builder.disableOpenDir();
                })
                .packageConfig(builder -> {
                    builder.parent("com.wanjing.mall.supply.admin") // 设置父包名
//                            .moduleName("system") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir")+"/src/main/resources/mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tableNames); // 设置需要生成的表名
                    builder.serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl");
                    builder.entityBuilder()
                            .enableLombok()
                            .idType(IdType.AUTO)
                            .naming(NamingStrategy.underline_to_camel)
                            .columnNaming(NamingStrategy.underline_to_camel);
                    builder.controllerBuilder()
                            .enableRestStyle()
                            .formatFileName("%sController");
                    builder.mapperBuilder()
                            .enableBaseResultMap()
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapper");
//                            .addTablePrefix("supply_"); // 设置过滤表前缀
                })
                .templateConfig(builder -> {
                    builder.disable(TemplateType.ENTITY);
                    if (isRuoYiGen){
                        builder.controller("/templates/ruoyi/controller.java");
                    }else {
                        builder.controller("/templates/controller.java");
                    }
                    builder.entity("/templates/entity.java");
                    builder.mapper("/templates/mapper.java");
                    builder.service("/templates/service.java");
                    builder.xml("/templates/mapper.xml");
                    builder.serviceImpl("/templates/serviceImpl.java");
                })
//                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();

    }
}
